package com.psp.bu.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.northend.util.NumUtil;
import com.northend.util.StringUtil;
import com.psp.bu.controller.res.BaseResult;
import com.psp.bu.controller.res.ListResult;
import com.psp.bu.controller.res.ObjectResult;
import com.psp.bu.controller.res.bean.RAreaListBean;
import com.psp.bu.controller.res.bean.RAttributeBean;
import com.psp.bu.controller.res.bean.RProductDetailBean;
import com.psp.bu.controller.res.bean.RProductEditDetailBean;
import com.psp.bu.controller.res.bean.RProductListBean;
import com.psp.bu.controller.res.bean.RProductSkuBean;
import com.psp.bu.controller.res.bean.RServicesTypeBean;
import com.psp.bu.controller.springmvc.req.GetAreaListParam;
import com.psp.bu.controller.springmvc.req.GetProductAttributesParam;
import com.psp.bu.controller.springmvc.req.GetProductInfoBySkunoParam;
import com.psp.bu.controller.springmvc.req.GetProductInfoParam;
import com.psp.bu.controller.springmvc.req.GetSubTypesParam;
import com.psp.bu.controller.springmvc.req.ProductDeleteParam;
import com.psp.bu.controller.springmvc.req.ProductDetailParam;
import com.psp.bu.controller.springmvc.req.ProductListParam;
import com.psp.bu.controller.springmvc.req.ProductOffShelfParam;
import com.psp.bu.controller.springmvc.req.ProductOnShelfParam;
import com.psp.bu.controller.springmvc.req.SaveProductParam;
import com.psp.bu.service.AddressService;
import com.psp.bu.service.ProductService;
import com.psp.bu.service.exception.ServiceException;
import com.psp.bu.service.impl.PageResult;

@Component
public class ProductController {

	Logger log = Logger.getLogger(ProductController.class);

	@Autowired
	AddressService addressServiceImpl;

	@Autowired
	ProductService productServiceImpl;

	/**
	 * 商品保存
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult save(SaveProductParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		String token = (String) request.getAttribute("token");
		String userId = (String) request.getAttribute("userId");
		boolean flag = false;
		try {
			int category = NumUtil.toInt(param.getTypeCode(), 0);
			int type = NumUtil.toInt(param.getType(), 1);
			String title = param.getTitle();
			String subTitle = param.getSubTitle();
			String img = param.getImg();
			int quoteType = NumUtil.toInt(param.getQuoteType(), 1);
			double price = NumUtil.toDouble(param.getPrice(), 0);
			String serviceContent = param.getServiceContent();
			String serviceSafe = param.getServiceSafe();
			String linkMan = param.getLinkMan();
			String telephone = param.getTelephone();
			String attributeAtrr = param.getAttributeArr();
			String attributeValueArr = param.getAttributeValueArr();
			String attributeJson = param.getAttributeJson();
			int hasInvocie = NumUtil.toInt(param.getHasInvoice(), 0);

			if (StringUtil.isEmpty(param.getBid())) {// 保存
				flag = productServiceImpl.save(userId, category, type, title, subTitle, img, quoteType, price,
						serviceContent, serviceSafe, linkMan, telephone, attributeJson, attributeAtrr,
						attributeValueArr, hasInvocie);
			} else {// 更新
				flag = productServiceImpl.update(userId, category, type, title, subTitle, img, quoteType, price,
						serviceContent, serviceSafe, linkMan, telephone, attributeJson, attributeAtrr,
						attributeValueArr, hasInvocie, param.getBid());
			}
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setServiceException(e);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.setServiceException(new ServiceException("exception_error", "发生异常"));
		}
		result.setToken(token);
		return result;
	}

	/**
	 * 业务组获取商品列表
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RProductListBean> list(ProductListParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RProductListBean> result = new ListResult<RProductListBean>();
		String token = (String) request.getAttribute("token");
		String userId = (String) request.getAttribute("userId");
		int page = NumUtil.toInt(param.getPage(), 0);
		int pageSize = NumUtil.toInt(param.getPageSize(), 10);
		int shelfState = NumUtil.toInt(param.getShelfState(), 0);

		PageResult<RProductListBean> resList = productServiceImpl.getList(page, pageSize, userId, shelfState);

		result.setData(resList.getData());
		result.setTotalSize(resList.getCount());
		result.setPage(page);
		result.setToken(token);
		return result;
	}

	/**
	 * 商品批量上架
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult onShelf(ProductOnShelfParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		String token = (String) request.getAttribute("token");
		try {
			String buserId = (String) request.getAttribute("userId");
			String pid = param.getPid();
			List<String> list = JSON.parseArray(pid, String.class);
			boolean flag = productServiceImpl.onShelfBatch(buserId, list);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		result.setToken(token);
		return result;
	}

	/**
	 * 商品批量下架
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult offShelf(ProductOffShelfParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		String token = (String) request.getAttribute("token");
		try {
			String userId = (String) request.getAttribute("userId");
			String pid = param.getPid();
			List<String> list = JSON.parseArray(pid, String.class);
			boolean flag = productServiceImpl.offShelfBatch(userId, list);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		result.setToken(token);
		return result;
	}

	/**
	 * 商品删除
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult delete(ProductDeleteParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		String token = (String) request.getAttribute("token");
		try {
			String userId = (String) request.getAttribute("userId");
			String pid = param.getPid();
			List<String> list = JSON.parseArray(pid, String.class);
			boolean flag = productServiceImpl.deleteBatch(userId, list);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		result.setToken(token);
		return result;
	}

	/**
	 * 获取商品详情
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RProductDetailBean> detail(ProductDetailParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RProductDetailBean> result = new ObjectResult<>();
		String token = (String) request.getAttribute("token");
		try {
			String buid = (String) request.getAttribute("userId");
			String pid = param.getPid();
			RProductDetailBean rbean = productServiceImpl.getProductDetail(buid, pid);
			result.setData(rbean);
			result.setFlag(rbean != null);
			result.setToken(param.getToken());
		} catch (ServiceException e) {
			result.setServiceException(e);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.setServiceException(new ServiceException("error", "发生异常"));
			result.setFlag(false);
		}
		result.setToken(token);
		return result;
	}

	/**
	 * 获取编辑时的商品详情
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RProductEditDetailBean> getProductInfo(GetProductInfoParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RProductEditDetailBean> result = new ObjectResult<>();
		String token = (String) request.getAttribute("token");
		try {
			String pid = param.getBid();
			String buid = (String) request.getAttribute("userId");
			RProductEditDetailBean rbean = productServiceImpl.getProductDetailDetail(buid, pid);
			result.setData(rbean);
			result.setFlag(true);
		} catch (ServiceException e) {
			result.setServiceException(e);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.setServiceException(new ServiceException("error", "发生异常"));
			result.setFlag(false);
		}
		result.setToken(token);
		return result;
	}

	/**
	 * 获取属性值
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RAttributeBean> getProductAttributes(GetProductAttributesParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RAttributeBean> result = new ListResult<RAttributeBean>();
		String token = (String) request.getAttribute("token");
		String pid = param.getPid();
		try {
			List<RAttributeBean> resData = productServiceImpl.getAttributes(pid);
			if (resData != null) {
				result.setData(resData);
				result.setTotalSize(resData.size());
			}
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setMsg(e.getServiceMsg());
		}
		result.setToken(token);
		return result;
	}

	public ObjectResult<RProductSkuBean> getProductInfoBySkuno(GetProductInfoBySkunoParam param,
			HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RProductSkuBean> result = new ObjectResult<RProductSkuBean>();
		String token = (String) request.getAttribute("token");
		String skuCode = param.getSkuCode();
		try {
			RProductSkuBean resData = productServiceImpl.getProductInfoBySkuno(skuCode);
			if (resData != null) {
				result.setData(resData);
			}
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setMsg(e.getServiceMsg());
		}
		result.setToken(token);
		return result;
	}

	/**
	 * 根据父节点获取
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RServicesTypeBean> getSubTypes(GetSubTypesParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RServicesTypeBean> result = new ObjectResult<>();
		String token = (String) request.getAttribute("token");
		int pcode = NumUtil.toInt(param.getCode(), 0);
		RServicesTypeBean bean = productServiceImpl.getSubServiceTypes(pcode);
		result.setData(bean);
		result.setSysTime(System.currentTimeMillis());
		result.setFlag(true);
		result.setToken(token);
		return result;
	}

	public ListResult<RAreaListBean> getAreas(GetAreaListParam param, HttpServletRequest request,
			HttpServletResponse response) {
		String adcode = param.getAdcode();
		String token = (String) request.getAttribute("token");
		ListResult<RAreaListBean> result = new ListResult<>();
		PageResult<RAreaListBean> resData = addressServiceImpl.getAreas(adcode);
		if (resData != null) {
			result.setTotalSize(resData.getCount());
			result.setData(resData.getData());
		}
		result.setFlag(true);
		result.setToken(token);
		return result;
	}
}
